﻿using Model;
using Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataProvider
{
    public class Sys_ModuleProvider
    {
        public static readonly Sys_ModuleProvider Instances = new Sys_ModuleProvider();
        public List<Sys_Module> GetTreeList()
        {
            var List = Sys_ModuleService.Instances.QueryCustom(" Status=0 ").OrderBy(x=>x.OrderNumber).ToList();
            var TopList = List.FindAll(x => x.ParentId == 0);
            foreach(var d in TopList)
            {
                d.children = List.FindAll(x => x.ParentId == d.Id);
                if(d.children!=null&&d.children.Count>0)
                {
                    d.isParent = true;
                    foreach (var children in d.children)
                    {
                        BuildChildren(children, List);
                    }
                }
                else
                {
                    d.isParent = false;
                }
                
            }
            return TopList;
            void BuildChildren(Sys_Module d, List<Sys_Module> allList)
            {
                d.children = List.FindAll(x => x.ParentId == d.Id);
                if (d.children != null && d.children.Count > 0)
                {
                    d.isParent = true;
                    foreach (var children in d.children)
                    {
                        BuildChildren(children, List);
                    }
                }
                else
                {
                    d.isParent = false;
                }
            }
        }
    }
}
